#!/usr/bin/env python
# -*- coding:UTF-8 -*-
# zhouyi 6098550@qq.com

import mi_speek
import mi_server
from bottle import route, run, template, request
import signal
import sys

# @route('/hello/<name>')
# def index(name):
#     return template('<b>hello {{name}}</b>', name=name)

@route('/')
def hello():
    return 'This is the mi machine api.bottle restful.'

@route('/v1/machine_uuid')
def machine_uuid():
    return '1234567890'

@route('/v1/weight/<no>')
def get_weight(no):
    return mi_server.GetWeight(int(no))

@route('/v1/start_work')
def start_work():
    return mi_server.StartWork()

@route('/v1/stop_work')
def stop_work():
    return mi_server.StopWork()

@route('/v1/abort_work')
def abort_work():
    return mi_server.AbortWork()

@route('/v1/work_status')
def work_status():
    return mi_server.WorkStatus()

@route('/v1/speek_text')
def speek_text():
    content = request.GET.get('content', '').strip()
    return mi_speek.SpeekText(content)

if __name__ == '__main__':
    #signal.signal(signal.SIGINT, handler)
    #signal.signal(signal.SIGTERM, handler)

    run(host='0.0.0.0', port=mi_server.config.flask_config.port)


def handler(signum, frame):
    global is_exit
    is_exit = True
    print "receive a signal %d, is_exit=%d" % (signum, is_exit)
    sys.exit()